# Copyright (C) 2024 Advanced Micro Devices, Inc.  All rights reserved.
# SPDX-License-Identifier: MIT
cmake_minimum_required(VERSION 3.15)
include(${CMAKE_CURRENT_SOURCE_DIR}/XilsecureExample.cmake)
project(xilsecure)

find_package(common)
enable_language(C ASM)
collect(PROJECT_LIB_DEPS xilstandalone)
collect(PROJECT_LIB_DEPS xil)
collect(PROJECT_LIB_DEPS xiltimer)
collect(PROJECT_LIB_DEPS xilsecure)
if(("${CMAKE_MACHINE}" STREQUAL "Versal") OR ("${CMAKE_MACHINE}" STREQUAL "VersalNet"))
    if("${XILSECURE_mode}" STREQUAL "client")
        collect(PROJECT_LIB_DEPS xilmailbox)
    endif()
endif()
collect(PROJECT_LIB_DEPS gcc)
collect(PROJECT_LIB_DEPS c)
collector_list (_deps PROJECT_LIB_DEPS)

if("${CMAKE_MACHINE}" STREQUAL "Versal")
    if("${XILSECURE_mode}" STREQUAL "server")
        SET(COMMON_EXAMPLES xilsecure_versal_aes_server_example.c;xilsecure_versal_ecdsa_server_example.c;xilsecure_versal_rsa_server_example.c;xilsecure_versal_sha_server_example.c;)
    else()
        SET(COMMON_EXAMPLES xilsecure_versal_aes_client_example.c;xilsecure_versal_ecdsa_client_example.c;xilsecure_versal_rsa_client_example.c;xilsecure_versal_sha_client_example.c;)
    endif()
elseif("${CMAKE_MACHINE}" STREQUAL "VersalNet")
    SET(COMMON_EXAMPLES xilsecure_versal_aes_client_example.c;xilsecure_versal_ecdsa_client_example.c;xilsecure_versal_rsa_client_example.c;xilsecure_versal_sha_client_example.c;
    xilsecure_versal_net_ecdh_client_example.c;xilsecure_versal_net_keyunwrap_client_example.c;xilsecure_versal_net_trng_client_example.c;)
elseif("${CMAKE_MACHINE}" STREQUAL "ZynqMP")
    SET(COMMON_EXAMPLES xilsecure_aes_example.c;xilsecure_rsa_example.c;xilsecure_rsa_generic_example.c;xilsecure_sha_example.c;xilsecure_simple_aes_example.c;)
endif()

linker_gen("${CMAKE_CURRENT_SOURCE_DIR}/../../../../scripts/linker_files/")
foreach(EXAMPLE ${COMMON_EXAMPLES})
    string(REGEX REPLACE "\\.[^.]*$" "" EXAMPLE ${EXAMPLE})
    add_executable(${EXAMPLE}.elf ${EXAMPLE})
    target_link_libraries(${EXAMPLE}.elf -Wl,--gc-sections -T\"${CMAKE_SOURCE_DIR}/lscript.ld\" -L\"${CMAKE_SOURCE_DIR}/\" -Wl,--start-group ${_deps} -Wl,--end-group)
endforeach()
